/**
 * 权限管理工具函数
 */
import { apiClients } from '@/services/apiClient';
import { autoLoginInDev } from './autoLogin';

/**
 * 获取用户权限列表
 * @param userId 用户ID
 * @returns 权限列表
 */
export const getUserPermissions = async (userId: number): Promise<string[]> => {
  try {
    await autoLoginInDev();
    
    const response = await apiClients.permissions.permissionsUserRolesUserPermissionsList({
      userId: userId
    });
    
    // 手动提取data字段
    const responseData = (response as any).data || response;
    return responseData?.permissions || [];
  } catch (error) {
    console.error('获取用户权限失败:', error);
    return [];
  }
};

/**
 * 获取用户角色列表
 * @param userId 用户ID
 * @returns 角色列表
 */
export const getUserRoles = async (userId: number): Promise<string[]> => {
  try {
    await autoLoginInDev();
    
    const response = await apiClients.permissions.permissionsUserRolesUserPermissionsList({
      userId: userId
    });
    
    // 手动提取data字段
    const responseData = (response as any).data || response;
    return responseData?.roles || [];
  } catch (error) {
    console.error('获取用户角色失败:', error);
    return [];
  }
};

/**
 * 检查用户是否有特定权限
 * @param permissions 用户权限列表
 * @param requiredPermission 需要检查的权限
 * @returns 是否有权限
 */
export const hasPermission = (permissions: string[], requiredPermission: string): boolean => {
  return permissions.includes(requiredPermission);
};

/**
 * 检查用户是否有任一权限
 * @param permissions 用户权限列表
 * @param requiredPermissions 需要检查的权限列表
 * @returns 是否有任一权限
 */
export const hasAnyPermission = (permissions: string[], requiredPermissions: string[]): boolean => {
  return requiredPermissions.some(permission => permissions.includes(permission));
};

/**
 * 检查用户是否有所有权限
 * @param permissions 用户权限列表
 * @param requiredPermissions 需要检查的权限列表
 * @returns 是否有所有权限
 */
export const hasAllPermissions = (permissions: string[], requiredPermissions: string[]): boolean => {
  return requiredPermissions.every(permission => permissions.includes(permission));
};

/**
 * 权限常量定义
 */
export const PERMISSIONS = {
  // 用户管理
  USERS_VIEW: 'users:view',
  USERS_LIST: 'users:list',
  USERS_CREATE: 'users:create',
  USERS_UPDATE: 'users:update',
  USERS_DELETE: 'users:delete',
  USERS_TOGGLE_STATUS: 'users:toggle_status',
  
  // 权限管理
  PERMISSIONS_VIEW: 'permissions:view',
  PERMISSIONS_LIST: 'permissions:list',
  PERMISSIONS_CREATE: 'permissions:create',
  PERMISSIONS_UPDATE: 'permissions:update',
  PERMISSIONS_DELETE: 'permissions:delete',
  
  // 角色管理
  ROLES_VIEW: 'roles:view',
  ROLES_LIST: 'roles:list',
  ROLES_CREATE: 'roles:create',
  ROLES_UPDATE: 'roles:update',
  ROLES_DELETE: 'roles:delete',
  ROLES_ASSIGN_PERMISSIONS: 'roles:assign_permissions',
  ROLES_ASSIGN_USERS: 'roles:assign_users',
  
  // 系统管理
  SYSTEM_ADMIN: 'system:admin',
  SYSTEM_SETTINGS: 'system:settings',
} as const;

/**
 * 权限组定义
 */
export const PERMISSION_GROUPS = {
  USER_MANAGEMENT: [
    PERMISSIONS.USERS_VIEW,
    PERMISSIONS.USERS_LIST,
    PERMISSIONS.USERS_CREATE,
    PERMISSIONS.USERS_UPDATE,
    PERMISSIONS.USERS_DELETE,
    PERMISSIONS.USERS_TOGGLE_STATUS,
  ],
  PERMISSION_MANAGEMENT: [
    PERMISSIONS.PERMISSIONS_VIEW,
    PERMISSIONS.PERMISSIONS_LIST,
    PERMISSIONS.PERMISSIONS_CREATE,
    PERMISSIONS.PERMISSIONS_UPDATE,
    PERMISSIONS.PERMISSIONS_DELETE,
  ],
  ROLE_MANAGEMENT: [
    PERMISSIONS.ROLES_VIEW,
    PERMISSIONS.ROLES_LIST,
    PERMISSIONS.ROLES_CREATE,
    PERMISSIONS.ROLES_UPDATE,
    PERMISSIONS.ROLES_DELETE,
    PERMISSIONS.ROLES_ASSIGN_PERMISSIONS,
    PERMISSIONS.ROLES_ASSIGN_USERS,
  ],
  SYSTEM_MANAGEMENT: [
    PERMISSIONS.SYSTEM_ADMIN,
    PERMISSIONS.SYSTEM_SETTINGS,
  ],
} as const;
